package com.newtype.meeting.client.mvc.frontend;

import java.util.List;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.store.GroupingStore;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.client.model.Meeting;
import com.newtype.meeting.client.model.MeetingNotify;
import com.newtype.meeting.client.model.MeetingRecord;
import com.newtype.meeting.client.model.MeetingReport;
import com.newtype.meeting.client.model.MeetingStart;
import com.newtype.meeting.client.mvc.AppView;
import com.newtype.meeting.client.widget.frontend.EditMeetingContentPanel;
import com.newtype.meeting.client.widget.frontend.EditOpenTypeWindow;
import com.newtype.meeting.client.widget.frontend.MeetingContentPanel;
import com.newtype.meeting.client.widget.frontend.MeetingNoticePanel;
import com.newtype.meeting.client.widget.frontend.MeetingPanel;
import com.newtype.meeting.client.widget.frontend.NamedTableWindow;
import com.newtype.meeting.shared.MeetingNotifyDTO;

public class MeetingView extends View {
	private MeetingPanel meetingPanel;
	private MeetingContentPanel meetingContentPanel;
	private EditMeetingContentPanel editMeetingContentPanel;
	private EditOpenTypeWindow editOpenTypeWindow;
	private NamedTableWindow namedTableWindow;
	private MeetingNoticePanel meetingNoticePanel;
	private LayoutContainer wrapper = Registry.get(AppView.CENTER_PANEL);

	@Override
	protected void initialize() {
		super.initialize();

		meetingPanel = new MeetingPanel();
	}

	public MeetingView(Controller controller) {
		super(controller);
	}

	@Override
	protected void handleEvent(AppEvent event) {
		if (event.getType().equals(AppEvents.ViewMeeting)) {
			onViewMeeting(event);
		} else if (event.getType().equals(AppEvents.ToMeetingContent)) {
			onToMeetingContent(event);
		} else if (event.getType().equals(AppEvents.ToEditMeetingContent)) {
			onToEditMeetingContent(event);
		} else if (event.getType().equals(AppEvents.ToEditOpenTypeWindow)) {
			onToEditOpenTypeWindow(event);
		} else if (event.getType().equals(AppEvents.ToNamedTableWindow)) {
			onToNamedTableWindow(event);
		} else if (event.getType().equals(AppEvents.ToMeetingNotice)) {
			onToMeetingNotice(event);
		}
	}

	/**
	 * 跳转至会议通知窗口
	 * @param event
	 */
	private void onToMeetingNotice(AppEvent event) {
		wrapper.removeAll();
		Meeting meeting = event.getData("meeting");
		MeetingNotify notify = event.getData("notify");
		meetingNoticePanel = new MeetingNoticePanel(meeting, notify);
		wrapper.add(meetingNoticePanel);
		wrapper.layout();
	}

	/**
	 * 打开点名表窗口
	 * @param event
	 */
	private void onToNamedTableWindow(AppEvent event) {
		namedTableWindow = new NamedTableWindow();
		//预订出席
		GroupingStore<MeetingNotifyDTO> subscribeStore = namedTableWindow
				.getSubscribeStore();
		subscribeStore.removeAll();
		subscribeStore.add(event.<List<MeetingNotifyDTO>> getData("subscribe"));

		//无法出席
		GroupingStore<MeetingNotifyDTO> incapableStore = namedTableWindow
				.getIncapableStore();
		incapableStore.removeAll();
		incapableStore.add(event.<List<MeetingNotifyDTO>> getData("incapable"));
		
		//尚未回复
		GroupingStore<MeetingNotifyDTO> notyetStore = namedTableWindow
				.getNotyetStore();
		notyetStore.removeAll();
		notyetStore.add(event.<List<MeetingNotifyDTO>> getData("notyet"));
		namedTableWindow.show();
	}

	/**
	 * 弹出修改会议公开对象窗口
	 * @param event
	 */
	private void onToEditOpenTypeWindow(AppEvent event) {
		Meeting meeting = event.getData();
		editOpenTypeWindow = new EditOpenTypeWindow(meeting);
		editOpenTypeWindow.show();
	}

	/**
	 * 执行跳转到修改会议内容窗口
	 * @param event
	 */
	private void onToEditMeetingContent(AppEvent event) {
		wrapper.removeAll();
		Meeting meeting = event.getData();

		editMeetingContentPanel = new EditMeetingContentPanel(meeting);
		wrapper.add(editMeetingContentPanel);
		wrapper.layout();
	}

	/** 跳转到会议内容面板 */
	private void onToMeetingContent(AppEvent event) {
		wrapper.removeAll();

		meetingContentPanel = new MeetingContentPanel(event
				.<Meeting> getData("meeting"));
		//会议报告
		ListStore<MeetingReport> reportStore = meetingContentPanel
				.getReportStore();
		reportStore.removeAll();
		reportStore.add(event.<List<MeetingReport>> getData("reports"));
		//会议记录
		ListStore<MeetingRecord> recordStore = meetingContentPanel
				.getRecordStore();
		recordStore.removeAll();
		recordStore.add(event.<List<MeetingRecord>> getData("records"));
		//会议通知
		GroupingStore<MeetingNotifyDTO> notifyStore = meetingContentPanel
				.getNotifyStore();
		notifyStore.removeAll();
		notifyStore.add(event.<List<MeetingNotifyDTO>> getData("notices"));

		wrapper.add(meetingContentPanel);
		wrapper.layout();
	}

	/** 浏览会议首页 */
	private void onViewMeeting(AppEvent event) {
		wrapper.removeAll();
		//会议通知
		ListStore<MeetingNotifyDTO> noticeStore = meetingPanel.getNoticeStore();
		noticeStore.removeAll();
		noticeStore.add(event.<List<MeetingNotifyDTO>> getData("notice"));
		//草稿
		ListStore<MeetingStart> draftStore = meetingPanel.getDraftStore();
		draftStore.removeAll();
		draftStore.add(event.<List<MeetingStart>> getData("draft"));
		//待召开会议
		ListStore<Meeting> heldtoStore = meetingPanel.getHeldtoStore();
		heldtoStore.removeAll();
		heldtoStore.add(event.<List<Meeting>> getData("heldto"));
		//历史会议history
		ListStore<Meeting> historyStore = meetingPanel.getHistoryStore();
		historyStore.removeAll();
		historyStore.add(event.<List<Meeting>> getData("history"));
		wrapper.add(meetingPanel);
		wrapper.layout();
	}
}
